package com.example.host.job;

import ch.qos.logback.core.net.ssl.SSL;
import com.alibaba.fastjson.JSONObject;
import com.example.host.config.MqttConfig;
import com.example.host.domain.SessionInfo;
import com.example.host.service.RemoteControlService;
import com.example.host.utils.SessionInfoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;

/**
 * 还未整合前端页面，使用定时任务模拟远控动作
 */
@Service
@EnableScheduling
@Slf4j
public class TestJob {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    RemoteControlService remoteControlService;
    @Autowired
    SessionInfoUtils sessionInfoUtils;
    @Autowired
    MqttConfig mqttConfig;

//    @Scheduled(cron = "*/5 * * * * ?")
    public void callService(){
        String hostAddress = "172.16.251.235";
        String port = "8080";
        String url = MessageFormat.format("http://{0}:{1}/installService",hostAddress,port);
        log.info(MessageFormat.format("向公网IP为{0}的远程主机发起远控请求！",hostAddress));
        JSONObject result = restTemplate.postForObject(url, "InstallService", JSONObject.class);
        if (result != null) {
            log.info("远控请求发起成功！远控会话信息：" + result);
            remoteControlService.openRemoteHtml(result);
        } else {
            log.info("远控请求发起失败！");
        }
        
    }
    
//    @Scheduled(cron = "*/5 * * * * ?")
    public void deleteService() {
        String hostAddress = "172.16.251.235";
        String port = "8080";
        String url = MessageFormat.format("http://{0}:{1}/uninstallService",hostAddress,port);
        log.info(MessageFormat.format("解除公网IP为{0}的远程主机的远控！",hostAddress));
        String result = restTemplate.postForObject(url, "UninstallService", String.class);
        if (result != null&&result.contains("成功")){
            log.info(MessageFormat.format("公网IP为{0}的远程主机的远控解除成功！",hostAddress));
        } else {
            log.info(MessageFormat.format("公网IP为{0}的远程主机的远控解除失败！",hostAddress));
        }
    }

    @Scheduled(cron = "*/5 * * * * ?")
    public void mqttInstallService(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceClientId","dachuan");
        jsonObject.put("instruction","install");
        String testString = jsonObject.toJSONString();
        System.out.println(testString);
        try {
            mqttConfig.publish(0,false,"t/ttt",testString);
            while (!sessionInfoUtils.isInstallSuccess("dachuan")){
                Thread.sleep(100);
            }
            JSONObject sessionInfo = sessionInfoUtils.getSessionInfo("dachuan");
            System.out.println("address:"+sessionInfo.getString("address") + "\ncid:" + sessionInfo.getString("cid"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @Scheduled(cron = "*/7 * * * * ?")
    public void mqttUninstallService(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceName","dachuan");
        jsonObject.put("instruction","uninstall");
        String testString = jsonObject.toJSONString();
        System.out.println(testString);
        try {
            mqttConfig.publish(0,false,"t/ttt",testString);
            while (sessionInfoUtils.isUninstallSuccess("dachuan")){
                Thread.sleep(100);
            }
            System.out.println(sessionInfoUtils.getSessionInfo("dachuan").getString("msg"));
            sessionInfoUtils.deleteSessionInfo("dachuan");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @Scheduled(cron = "*/5 * * * * ?")
    public void test(){
        System.out.println(SessionInfo.sessionList);
    }
}
